home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 014 / termcap / tputs.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  5KB  |  213 lines

  1. /************************************************************************
  2.  *                                    *
  3.  *            Copyright (c) 1982, Fred Fish            *
  4.  *                All Rights Reserved                *
  5.  *                                    *
  6.  *    This software and/or documentation is released for public    *
  7.  *    distribution for personal, non-commercial use only.        *
  8.  *    Limited rights to use, modify, and redistribute are hereby    *
  9.  *    granted for non-commercial purposes, provided that all        *
  10.  *    copyright notices remain intact and all changes are clearly    *
  11.  *    documented.  The author makes no warranty of any kind with    *
  12.  *    respect to this product and explicitly disclaims any implied    *
  13.  *    warranties of merchantability or fitness for any particular    *
  14.  *    purpose.                            *
  15.  *                                    *
  16.  ************************************************************************
  17.  */
  18.  
  19.  
  20. /*
  21.  *  LIBRARY FUNCTION
  22.  *
  23.  *    tputs     output string with appropriate padding
  24.  *
  25.  *  KEY WORDS
  26.  *
  27.  *    termcap
  28.  *
  29.  *  SYNOPSIS
  30.  *
  31.  *    tputs(cp,affcnt,outc)
  32.  *    char *cp;
  33.  *    int affcnt;
  34.  *    int (*outc)();
  35.  *
  36.  *  DESCRIPTION
  37.  *
  38.  *    Outputs string pointed to by cp, using function outc, and
  39.  *    following it with the appropriate number of padding characters.
  40.  *    Affcnt contains the number of lines affected, which is used
  41.  *    as a multiplier for the specified per line pad time.  If
  42.  *    per line pad count is not applicable, affcnt should be 1,
  43.  *    NOT zero.
  44.  *
  45.  *    The format of the string pointed to by cp is:
  46.  *
  47.  *        [pad time][*]<string to send>
  48.  *
  49.  *        where:    pad time => time to delay in milliseconds
  50.  *            * => specifies that time is per line
  51.  *            
  52.  *    The pad character is assumed to reside in the external
  53.  *    variable "PC".  Also, the external variable "ospeed"
  54.  *    should contain the output speed of the terminal as
  55.  *    encoded in /usr/include/sgtty.h  (B0-B9600).
  56.  *
  57.  *  BUGS
  58.  *
  59.  *    Digit conversion is based on native character set
  60.  *    being ASCII.
  61.  *
  62.  */
  63.  
  64. /*
  65.  *    Miscellaneous stuff
  66.  */
  67.  
  68. #include <stdio.h>
  69.  
  70. extern char PC;            /* Pad character to use */
  71. extern short ospeed;        /* Encoding of output speed */
  72.  
  73. static int times[] = {
  74.     0,                /* Tenths of ms per char 0 baud */
  75.     2000,            /* Tenths of ms per char 50 baud */
  76.     1333,            /* Tenths of ms per char 75 baud */
  77.     909,            /* Tenths of ms per char 110 baud */
  78.     743,            /* Tenths of ms per char 134 baud */
  79.     666,            /* Tenths of ms per char 150 baud */
  80.     500,            /* Tenths of ms per char 200 baud */
  81.     333,            /* Tenths of ms per char 300 baud */
  82.     166,            /* Tenths of ms per char 600 baud */
  83.     83,                /* Tenths of ms per char 1200 baud */
  84.     55,                /* Tenths of ms per char 1800 baud */
  85.     41,                /* Tenths of ms per char 2400 baud */
  86.     20,                /* Tenths of ms per char 4800 baud */
  87.     10                /* Tenths of ms per char 9600 baud */
  88. };
  89.  
  90.  
  91. /*
  92.  *  PSEUDO CODE
  93.  *
  94.  *    Begin tgoto
  95.  *        If string pointer is invalid then
  96.  *        Return without doing anything.
  97.  *        Else
  98.  *        For each pad digit (if any)
  99.  *            Do decimal left shift.
  100.  *            Accumulate the lower digit.
  101.  *        End for
  102.  *        Adjust scale to tenths of milliseconds
  103.  *        If there is a fractional field
  104.  *            Skip the decimal point.
  105.  *            If there is a valid tenths digit
  106.  *            Accumulate the tenths.
  107.  *            End if
  108.  *            Discard remaining digits.
  109.  *        End if
  110.  *        If per line is specified then
  111.  *            Adjust the pad time.
  112.  *            Discard the per line flag char.
  113.  *        End if
  114.  *        While there are any characters left
  115.  *            Send them out via output function.
  116.  *        End while
  117.  *        Transmit any padding required.
  118.  *        End if
  119.  *    End tgoto
  120.  *
  121.  */
  122.  
  123. tputs(cp,affcnt,outc)
  124. char *cp;
  125. int affcnt;
  126. int (*outc)();
  127. {
  128.     int ptime;            /* Pad time in tenths of milliseconds */
  129.     extern int isdigit();
  130.  
  131.     if (cp == NULL || *cp == NULL) {
  132.     return;
  133.     } else {
  134.     for (ptime = 0; isdigit(*cp); cp++) {
  135.         ptime *= 10;
  136.         ptime += (*cp - '0');
  137.     }
  138.     ptime *= 10;
  139.     if (*cp == '.') {
  140.         cp++;
  141.         if (isdigit(*cp)) {
  142.         ptime += (*cp++ - '0');
  143.         }
  144.         while (isdigit(*cp)) {cp++;}
  145.     }
  146.     if (*cp == '*') {
  147.         ptime *= affcnt;
  148.         cp++;
  149.     }
  150.     while (*cp != NULL) {
  151.         (*outc)(*cp++);
  152.     }
  153.     do_padding(ptime,outc);
  154.     }
  155. }
  156.  
  157. /*
  158.  *  FUNCTION
  159.  *
  160.  *    do_padding    transmit any pad characters required
  161.  *
  162.  *  SYNOPSIS
  163.  *
  164.  *    static do_padding(ptime,outc)
  165.  *    int ptime;
  166.  *    int (*outc)();
  167.  *
  168.  *  DESCRIPTION
  169.  *
  170.  *    Does any padding required as specified by ptime (in tenths
  171.  *    of milliseconds), the output speed given in the external
  172.  *    variable ospeed, and the pad character given in the
  173.  *    external variable PC.
  174.  *
  175.  */
  176.  
  177. /*
  178.  *  PSEUDO CODE
  179.  *
  180.  *    Begin do_padding
  181.  *        If there is a non-zero pad time then
  182.  *        If the external speed is in range then
  183.  *            Look up the delay per pad character.
  184.  *            Round pad time up by half a character.
  185.  *            Compute number of characters to send.
  186.  *            For each pad character to send
  187.  *            Transmit the pad character.
  188.  *            End for
  189.  *        End if
  190.  *        End if
  191.  *    End do_padding
  192.  *
  193.  */
  194.  
  195. static do_padding(ptime,outc)
  196. int ptime;
  197. int (*outc)();
  198. {
  199.     register int nchars;
  200.     register int tpc;
  201.  
  202.     if (ptime != 0) {
  203.     if (ospeed >= 0 && ospeed <= (sizeof(times)/ sizeof(int))) {
  204.         tpc = times[ospeed];
  205.         ptime += (tpc / 2);
  206.         nchars = ptime / tpc;
  207.         for ( ; nchars > 0; --nchars) {
  208.         (*outc)(PC);
  209.         }
  210.     }
  211.     }
  212. }
  213.